<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 文本输入框和添加商品按钮 -->
      <el-input
        placeholder="请输入内容"
        v-model="query"
        clearable
        @clear="getGoodsList()"
        @keyup.enter.native="getGoodsList()"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getGoodsList()"
        ></el-button>
      </el-input>
      <el-button type="primary" @click="goAddPage">添加商品</el-button>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList" style="width: 100%;" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="180px">
          <template #default="{row}">
            {{ row.upd_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsList(row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      query: '',
      pagenum: 1,
      pagesize: 10,
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        },
      });
      if (res.meta.status !== 200) {
        return this.$Elementui.Message.error(res.meta.msg);
      }
      console.log(res.data);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 底部分页功能
    // 控制每页展示多少条数据
    handleSizeChange(newSize) {
      console.log(newSize);
      this.pagesize = newSize;
      this.pagenum = 1;
      this.getGoodsList();
    },
    // 控制当前显示的页码值
    handleCurrentChange(newNum) {
      this.pagenum = newNum;
      this.getGoodsList();
    },
    // 删除商品列表数据
    async removeGoodsList(id) {
      const comfirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => {
        return err;
      });
      if (comfirmResult !== 'confirm') {
        return this.$Elementui.Message.info('您已经取消了删除！');
      }
      const { data: res } = await this.$http.delete(`goods/${id}`);
      if (res.meta.status !== 200) {
        return this.$Elementui.Message.error(res.meta.msg);
      }
      this.getGoodsList();
      this.$Elementui.Message.success('删除成功！');
    },
    // 跳转至添加商品页面
    goAddPage() {
      this.$router.push('/goods/add');
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
  .el-input {
    width: 350px;
    margin-right: 10px;
  }
  .el-table {
    margin-top: 15px;
  }
  .el-pagination {
    margin-top: 15px;
  }
}
</style>
